// 同类型
function getkind(){
    $.get("../php/phpe/getGoodsListNew.php?typeId=006&count=8",function(data){
        let goodlist = JSON.parse(data);
        // console.log(goodlist);
        let htmlSrc = ''
        goodlist.forEach(goods=>{
            htmlSrc+=`
            <li class="recommend-item">
                <div class="break">
                    <img src="../${goods.goodsImg}">
                    <div class="recom-name">${goods.goodsName}</div>
                    <div class="recom-price">${goods.goodsPrice}元</div>
                    <div class="recom-tops">68.6万人好评</div>
                </div>
                <div class="recom-action">
                    <a href="javascript:;" class="ben-small" onclick="location.href = 'xiangqin.html?goodsId=${goods.goodsId}'">
                        加入购物车
                    </a>
                </div>
                <div class="recom-notiae">
                    <a href="#" class="btn-block">成功加入购物车</a>
                </div>
            </li>
            `
        })
        $('.recommend-list').html(htmlSrc)
    })
}
$(function(){
    getkind()
    getinfo()
})

console.log(getCookie('username'));
document.cookie;

// 从详情页接受
function getinfo(){
    $.get("../php/phpe/getShoppingCart.php?vipName="+getCookie('username'),function(data){
        let goodlist = JSON.parse(data);
        let htmlSrc = '';
        goodlist.forEach(goods=>{
            htmlSrc+=`
            <div class="item-box">
                <div class="item-row">
                    <div class="col col-check">
                        <input type="checkbox" class="iconfont icon-zhifangkuang icon_dan" checked>
                    </div>
                    <div class="col col-img"><img src="../${goods.goodsImg}" class="img-smell"></div>
                    <div class="col col-name">
                        <h3 class="name">${goods.goodsName}</h3>
                    </div>
                    <div class="col col-price">${goods.goodsPrice}</div>
                    <div class="col col-num">
                        <div class="num-nei">
                            <input type="button" value="-" class="ipt col-butd" typeid=${goods.goodsId}><input type="text" value="${goods.goodsCount}" class="ipt col-txt"><input type="button" value="+" class="ipt col-butu" typeid=${goods.goodsId}>
                        </div>
                    </div>
                    <div class="col col-total teaple">${goods.goodsPrice*goods.goodsCount}</div>
                    <div class="col col-action">
                        <span class="iconfont icon-cross" id=${goods.goodsId}></span>
                    </div>
                </div>
            </div>
            `
        })
        $('.list-body').html(htmlSrc)
        getcheck()
        getchecked()
        // 同步数据
        $('.cart-gon').html(getNum())
        $('.cart-jian').html(getche())
        $('.eveam').html(zongjia())
    })
}

// 继续购物
$('.back').on('click',function(){
    location.href = 'xiaomi.html'
})

// 点击复选框
function getcheck(){
    $('.icon_quan').on('change',function(){
        $('.icon_dan').prop('checked',this.checked)
    })
    $('.cart-jian').html(getche())
}
function getchecked(){
    $('.icon_dan').on('change',function(){
        if ($('.icon_dan:checked').length==$('.icon_dan').length) {
            $('.icon_quan').prop('checked',true)
        }else(
            $('.icon_quan').prop('checked',false)
        )
    })
}

// 选中内容变化
$('.icon_dan').each((index,item)=>{
    $(item).on('change',function(){
        console.log(item);
        getchecked()
        $('.cart-gon').html(getNum())
        $('.cart-jian').html(getche())
        $('.eveam').html(zongjia())
    })
})

// 修改购物车中的数量

// 
function gethome(){
    $.ajax({
        url:'../php/phpe/updateGoodsCount.php',
        type:'GET',
        data:{
            vipName:getCookie('username'),
            goodsId:theat.attr('typeid'),
            goodsCount:goodsCount
        },
        success(res){
            if (res==1) {
                console.log('修改成功');
            }
        }
    })
}

 // 加 
$('.list-body').on('click','.col-butu',function(){
    theat = $(this)
    jia()
    goodsCount = theat.prev().val()
    gethome()
    $('.cart-gon').html(getNum())
    $('.cart-jian').html(getche())
    $('.eveam').html(zongjia())
})
function jia(){
    var num = theat.prev().val();
    var danjia = theat.parent().parent().prev().html();
    var xiaoji = theat.parent().parent().next();
    num++
    theat.prev().val(num)
    xiaoji.html(num*danjia)
}
// 减
$('.list-body').on('click','.col-butd',function(){
    theat = $(this)
    if (theat.next().val()==1) {
        theat.next().val(1)
    }else{
        jian()
    }
    goodsCount = theat.next().val()
    gethome()
    $('.cart-gon').html(getNum())
    $('.cart-jian').html(getche())
    $('.eveam').html(zongjia())
})
function jian(){
    var num = theat.next().val();
    var danjia = theat.parent().parent().prev().html();
    var xiaoji = theat.parent().parent().next();
     num--
    theat.next().val(num)
    xiaoji.html(num*danjia) 
}   
// 共几件商品
function getNum(){
    var num = 0;
    // console.log($('.col-txt'));
    $('.col-txt').each((index,item)=>{
        // console.log(item);
        num+=parseInt($(item).val())
    })
    return num
}
// 选择的商品
function getche(){
    var n = 0;
    $('.col-txt').each((index,item)=>{
        if($(item).parent().find('.col-check').find('.icon_dan').is(':checked')==true){
        n+=parseInt($(item).val())
        }else{
            n=0
        }
    })
    return n
}
// 总价
function zongjia(){
    var sum = 0;
    $('.teaple').each((index,item)=>{
        if($(item).parent().find('.col-check').find('.icon_dan').is(':checked')==true){
            sum+=parseInt($(item).html())
        }else{
            sum = 0;
        }
    console.log($(item).parent().find('.col-check').find('.icon_dan').is(':checked'));
    })
    
    return sum;
}
// 删除
function shanchu(){
    $.ajax({
        url:"../php/phpe/deleteGoods.php",
        type:'GET',
        data:{
            vipName:getCookie('username'),
            goodsId:that.attr('id')
        },
        success(res){
            console.log(res);
        }
    })
}
let that;
$('.list-body').on('click','.icon-cross',function(){
    $('.mi-popup').css('display','block')
    that = $(this)
})
$('.btn-prarmy').on('click',function(){
    $('.mi-popup').css('display','none') 
        that.parent().parent().parent().remove()
        shanchu()
        var shuzhi = that.parent().prev().prev().children().children().eq(1).val()
        $('.cart-gon').html($('.cart-gon').html()-shuzhi)
        $('.cart-jian').html($('.cart-jian').html()-shuzhi)
        var xiaoti = that.parent().prev().html()
        $('.eveam').html($('.eveam').html()-xiaoti)
})  
$('.btn-gray').on('click',function(){
    $('.mi-popup').css('display','none')
})

// 关闭弹出框
$('.icon-cross').on('click',function(){
    $('.mi-popup').css('display','none')
})













